package com.gxa.eshops.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gxa.eshops.admin.service.AdminService;
import com.gxa.eshps.cores.common.JsonResult;
import com.gxa.eshps.cores.exception.BusinessException;
import com.gxa.eshps.cores.exception.ExceptionCode;
import com.gxa.eshps.cores.pojo.vo.AdminVO;
import com.gxa.eshps.cores.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.util.Map;

/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: Mr.Wang
 * @create: 2022-07-04 10:26
 **/
@Api(tags = "管理员接口文档")
@RestController
@RequestMapping("/admin")
@Validated
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    @Qualifier("jmsMeTemplate")
    JmsMessagingTemplate jmsMessagingTemplate;

    private static final String GOODS_CACHE = "goodsCache";

    LambdaQueryWrapper<AdminVO> lambda = Wrappers.lambdaQuery(AdminVO.class);

    @PostMapping("login")
    @ApiOperation("登录接口")
    public Map<String, Object> login(@RequestParam @Pattern(regexp = "^((13[0-9])|(15[^4,\\D])|(14[57])|(17[0])|(17[7])|(18[0,0-9]))\\d{8}$") String phone
            , @RequestParam String pwd) {
        Subject subject = SecurityUtils.getSubject();
        JsonResult jsonResult = new JsonResult();
        if (!subject.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken(phone, MD5Util.encode(pwd));
            subject.login(token);
            jsonResult.setCode("200");
            jsonResult.setMsg("登录成功");
        } else {
            jsonResult.setMsg("无需再次登录");
        }
        return jsonResult.getMap();
    }

    @PostMapping("/add")
    @ApiOperation("管理员注册接口")
    @RequiresRoles("admin")
    public Map<String, Object> register(@NotEmpty @RequestParam String phone, @RequestParam String pwd) {
        lambda.in(AdminVO::getPhone, phone).in(AdminVO::getPwd, MD5Util.encode(pwd));
        AdminVO adminVO = new AdminVO();
        adminVO.setPhone(phone);
        adminVO.setPwd(MD5Util.encode(pwd));
        adminService.saveOrUpdate(adminVO, lambda);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("注册成功");
        return jsonResult.getMap();
    }

    @PostMapping("/selectById")
    public Map<String, Object> selectById(Long id) throws BusinessException {
        AdminVO byId = adminService.getById(id);
        JsonResult jsonResult = new JsonResult();
        if (byId == null) {
            throw new BusinessException(ExceptionCode.GOODS_NULL);
        } else {
            jsonResult.setCode("1");
            jsonResult.setData(byId);
        }
        return jsonResult.getMap();
    }

}
